package com.fc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.fc.entity.SysRole;
import com.fc.service.SysRoleService;


import org.springframework.stereotype.Controller;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zffc
 * @since 2023-07-18
 */
@Controller
@RequestMapping("/sysRole" )
    public class SysRoleController {


        @Autowired
        private SysRoleService sysRoleService;

        @GetMapping("/toIndex" )
        public ModelAndView index(){
           ModelAndView mv=new ModelAndView();
           //因为配置了自带的视图解析器，所以默认访问template目录
           mv.setViewName("/sysRole/list" );
           return mv;
        }

        @GetMapping("/toAdd" )
        public ModelAndView toAdd(){
           ModelAndView mv=new ModelAndView();
           mv.setViewName("/sysRole/form" );
           return mv;
        }

        @GetMapping("/toEdit/{id}" )
        public ModelAndView toEdit(@PathVariable String id){//路径参数
            ModelAndView mv=new ModelAndView();
            SysRole sysRole = sysRoleService.getById(id);
            mv.setViewName("/sysRole/form" );
            mv.addObject("sysRole" ,sysRole);
            return mv;
        }


         /**
         *
         * @param sysRole
         * @param pageNumber
         * @param pageSize
         * @return
         */
         @GetMapping("/list" )
         @ResponseBody
         public Result<?> list(SysRole  sysRole,
                @RequestParam(name = "pageNumber" , defaultValue = "1" ) Integer pageNumber,
                @RequestParam(name = "pageSize" , defaultValue = "10" ) Integer pageSize){
           Page<SysRole> page=new Page(pageNumber,pageSize);
           return Result.OK(sysRoleService.page(page));
        }

         /**
         * 添加或修改方法
         * @param sysRole
         * @return
         */
         @PostMapping("/addOrEdit" )
         @ResponseBody
         public Result<?> addOrEdit(@RequestBody SysRole sysRole){
            sysRoleService.saveOrUpdate(sysRole);
            return Result.OK("ok" );
         }

         /**
         * 删除
         * @param id
         * @return
         */
         @GetMapping("/delete/{id}" )
         @ResponseBody
         public Result<?> delete(@PathVariable String id){
            sysRoleService.removeById(id);
            return Result.OK("ok" );
         }


}

